/**
 * 
 */
package org.hackystat.projecttrajectory.resource.estimate;

import java.io.IOException;
import org.hackystat.projecttrajectory.resource.ProjectTrajectoryResource;
import org.hackystat.projecttrajectory.resource.estimate.jaxb.ProjectEstimate;
import org.hackystat.utilities.stacktrace.StackTrace;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Variant;

/**
 * Implements the Resource for processing GET {host}/estimate/{user}/{project}
 * requests to obtain a summary of the given project for the given user.
 * 
 * @author Cam Moore
 * 
 */
public class EstimateResource extends ProjectTrajectoryResource {

  /**
   * The standard constructor.
   * 
   * @param context The context.
   * @param request The request object.
   * @param response The response object.
   */
  public EstimateResource(Context context, Request request, Response response) {
    super(context, request, response);
  }

  /**
   * 
   * @param variant The representational variant requested.
   * @return The representation.
   * @see org.hackystat.projecttrajectory.resource.ProjectTrajectoryResource#getRepresentation
   */
  @Override
  public Representation getRepresentation(Variant variant) {
    if (variant.getMediaType().equals(MediaType.TEXT_XML)) {
      String xmlData = null;
      // need to check the values to determine the type of GET
      // estimate/{user}/{project}/{tstamp}
      if (this.project == null) {
        // estimate/{user}
        xmlData = super.estimateManager.getUserEstimateIndex(uriUser);
      } else if (this.timestamp == null) {
        // estimate/{user}/{project} 
        xmlData = super.estimateManager.getEstimate(uriUser, project);
      } 
      return super.getStringRepresentation(xmlData);
    }
    return null;
  }

  /**
   * Indicate the PUT method is supported.
   * 
   * @return True.
   */
  @Override
  public boolean allowPut() {
    return true;
  }

  /**
   * Implement the PUT method that creates a new sensor data instance.
   * <ul>
   * <li> The XML must be marshallable into a sensor data instance.
   * <li> The timestamp in the URL must match the timestamp in the XML.
   * <li> The User and SDT must exist.
   * </ul>
   * Note that we are not validating that this sensor data instance contains all
   * of the Required Properties specified by the SDT. This should be done later,
   * on demand, as part of analyses.
   * <p>
   * We are also not at this point checking to see whether the User and SDT
   * exist.
   * 
   * @param entity The XML representation of the new sensor data instance..
   */
  @Override
  public void put(Representation entity) {
    // Get the payload.
    String entityString = null;
    try {
      entityString = entity.getText();
    } catch (IOException e) {
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Bad payload.");
      return;
    }
    putEstimate(entityString);
  }

  /**
   * Puts a project estimate XML payload.
   * 
   * @param entityString An entity string that should represent a
   *          ProjectEstimate.
   */
  private void putEstimate(String entityString) {
    ProjectEstimate estimate;
    try {
      estimate = this.estimateManager.makeProjectEstimate(entityString);
      this.estimateManager.putEstimate(estimate);
    } catch (Exception e) {
      server.getLogger().warning("Bad Project Estimate in PUT: " + StackTrace.toString(e));
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST,
          "Bad Project Estimate: " + entityString);
      return;
    }

  }

}
